<?php
require_once ROOT_PATH . '/lib/models/surveys/Answers.php';//Chinh Nguyen add
require_once ROOT_PATH . '/lib/models/surveys/Options.php';//Chinh Nguyen add
require_once ROOT_PATH . '/lib/models/surveys/Questions.php';//Chinh Nguyen add
require_once ROOT_PATH . '/lib/models/surveys/Surveys.php';//Chinh Nguyen add  
require_once ROOT_PATH . '/lib/models/hrfunct/EmpInfo.php';
require_once ROOT_PATH . '/FirePHPCore/FirePHP.class.php';
include ROOT_PATH.'/plugins/FusionChartsFree/Code/PHPClass/Includes/FusionCharts_Gen.php';
?>
 
<h2><?php echo $records->getSurveyName(); ?><hr/></h2>
<form id="frmCancelLeave" name="frmCancelLeave" method="post" action="">
<table border="0" cellpadding="0" cellspacing="0">
  <thead>
  	<tr>
		<th class="tableTopLeft"></th>
		<th class="tableTopMiddle" width="10"></th>
		<th class="tableTopMiddle" ></th>
		<th class="tableTopMiddle"></th>
    	<th class="tableTopMiddle" width="87"></th>
		<th class="tableTopRight"></th>
	</tr>
  </thead>
  <tbody>
<?php
	$i = 0;
		$max1=1;	
	$questions = Questions::getQuestionsBySurvey($records->getId());
	 	if(count($questions) >0){ ?>
	 		<?php foreach($questions as $question){
			 	if(!($i%2)) {$cssClass = 'odd';
			 	} else {  	$cssClass = 'even'; }
			 	$i++;
			 	$questionType= $question->getAnswerType();
		 	
			 	//$options = Options::getOptionsByQuestions($question->getId());
			 	$answers = Answers::getAnswersByQuestionSurvey($records->getId(),$question->getId());
		 	
		 	?>
				 <tr>
				  	<td class="tableMiddleLeft"></td>
				  	<td></td>
				    <td colspan ="2" class="<?php echo $cssClass; ?>" style="color:blue;font-size:12px"><?php echo "Question $i: ".$question->getText(); ?></td>
				  	<td></td>
					<td class="tableMiddleRight"></td>
				 </tr>	
	 			<?php  if($questionType == 4){
	 				if(count($answers)==0){?>
						<tr>
						  	<td class="tableMiddleLeft"></td>
						  		<td></td>
						  			<td></td>						  	
						  	<td > No Answer</td>
						    <td ></td>
							<td class="tableMiddleRight"></td>
					  	</tr>		
	 					
	 				<?php }else{
	 					
	 				foreach($answers as $answer){ ?>			 		
				 		<tr>
						  	<td class="tableMiddleLeft"></td>
						  	<td></td>
						  						  	
						  	<td > <?php echo "[".EmpInfo::getFullName($answer->getUser())."]:"; ?></td>
						    <td colspan="1"><?php echo $answer->getValue(); ?></td>
						    <td></td>
							<td class="tableMiddleRight"></td>
					  	</tr>		
	 				<?php } 
	 				} }else if($questionType == 1){
	 					$answerY = $answerN =0; 
	 					$unit1=0;	 				
	 					foreach($answers as $answer){
	 						if($answer->getValue()==1)$answerY++;
	 						else $answerN++;
	 					}
	 					$unit1 = $answerY;
	 					if($unit1 < $answerN)
	 						$unit1 = $answerN;
	 					if($max1>=60)		 
	 						$max1 = $max1/12;
	 					elseif($max1>=50)		 
	 						$max1 = $max1/10;		 					
	 					elseif($max1>=40)			 
	 						$unit1 = $unit1/8;
	 					elseif($unit1>=30)
	 						$unit1 = $unit1/6;
	 					elseif($unit1>=20)
	 						$unit1 = $unit1/4; 
 												
	 					$FC = new FusionCharts("Bar2D","600","100");									  
					 	$FC->setSwfPath("../../plugins/FusionChartsFree/Charts/");
					 	//if($unit1==0)
					  		//$strParam="caption=".$question->getText().";xAxisName=;yAxisName=Count;decimalPrecision=0; numDivLines=0;formatNumberScale=0";
					  	//else
					  	  	$strParam="caption=;xAxisName=;yAxisName=;decimalPrecision=0;numDivLines=$unit1;formatNumberScale=0";
					  	$FC->setChartParams($strParam);
					  	$FC->addChartData($answerY,"name=YES");
					  	$FC->addChartData($answerN,"name=NO");
					  
	 					?>
	 					<tr>
						  	<td class="tableMiddleLeft"></td>
						  	<td></td>
						  
						  	<?php if($unit1!=0){ ?>
						  	<td colspan="3"><?php $FC->renderChart(); ?> <?php // echo "Yes/No"; ?></td>
						  	<?php }else{ ?>
						  	<td colspan="3"> <?php  echo "No answer"; ?></td>
						  	<?php } ?>
						    <!-- <td class=""><?php //echo $answerY."/".$answerN ;?></td>  -->
							<td class="tableMiddleRight"></td>
					  	</tr>			 					 
 				<?php } else if(($questionType == 2 || $questionType == 3)){
	 						if(count($answers)==0){?>
		 						<tr>
							  	<td class="tableMiddleLeft"></td>
							  	<td></td>
							  	<td></td>								  	
							    <td colspan="2" class="">No answer</td>
								<td class="tableMiddleRight"></td>
						  		</tr>
	 						<?php	
	 						}else{
	 					$options = Options::getOptionsByQuestions($question->getId());
	 				
	 					if(count($options)>0){
	 						$width = count($options)*50;
	 						$FC = new FusionCharts("Bar2D","600",$width);
	 						
	 						$max1=1;									  
		 					foreach($options as $option){
		 						$answerCheck=0;
		 						foreach($answers as $answer){
		 							if($answer->getValue()==$option->getId())
		 								$answerCheck++;
		 						}
		 						$strOption=$option->getText();
		 						/*
		 						if(strlen($strOption)>=50)
		 							$strOption= substr($strOption,50)."(...)";
								*/
		 																			 						
		 						$FC->addChartData($answerCheck,"name=".$strOption);
		 						if($max1<$answerCheck)
		 							$max1 =$answerCheck;
		 						?>
		 					<?php }
		 					if($max1>=60)		 
		 						$max1 = $max1/12;
		 					elseif($max1>=50)		 
		 						$max1 = $max1/10;		 					
		 					elseif($max1>=40)		 
		 						$max1 = $max1/8;
		 					elseif($max1>=30)
		 						$max1 = $max1/6;
		 					elseif($max1>=10)
		 						$max1 = $max1/4; 	 					
		 					$FC->setSwfPath("../../plugins/FusionChartsFree/Charts/");
						  	$strParam="maxLabelWidthPercent=15; decimalPrecision=0;numDivLines=$max1;formatNumberScale=0;";
						  	$FC->setChartParams($strParam);				 							
						   
	 					}
	 						
	 					
	 					?>
	 					<tr>
						  	<td class="tableMiddleLeft"></td>
						  	<td></td>
						  									  	
						    <td colspan="3" class=""><?php $FC->renderChart(); ?></td>
							<td class="tableMiddleRight"></td>
					  	</tr>
				<?php }	
 				} 			
	 			}
	 		}
?>
  
  </tbody>
  <tfoot>
  	<tr>
		<td class="tableBottomLeft"></td>
		<td class="tableBottomMiddle"></td>
		<td class="tableBottomMiddle"></td>
		<td class="tableBottomMiddle"></td>
		<td class="tableBottomMiddle"></td>
		<td class="tableBottomRight"></td>
	</tr>
  </tfoot>
</table>

